//package com.xiaocikuai.smallchunks.controller.User;
//
//import com.xiaocikuai.smallchunks.entity.User.Login;
//import com.github.qcloudsms.SmsSingleSender;
//import com.github.qcloudsms.SmsSingleSenderResult;
//import java.util.Random;
//import java.time.LocalDateTime;
//import java.time.format.DateTimeFormatter;
//
//
//import com.xiaocikuai.smallchunks.utils.resultd.Resultd;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.cache.Cache;
//import org.springframework.cache.jcache.JCacheCacheManager;
//import org.springframework.web.bind.annotation.*;
//
////sms 短信服务
//@RequestMapping("/api/code")
//@CrossOrigin
//@RestController
//public class SmsController {
//
//
//    @Autowired
//    private JCacheCacheManager cacheManager;
//
//    @Qualifier("userMapper")
//
////    发送信息
//    @PostMapping("/send")
//    public Resultd sendCode(@RequestBody Login login){
//        try{
//            int appid = 1400912352;
//            String appKey = "15601612830bbf62d36db75fd532e796";
//            int templateId = 2162562;
//            String smsSign = "郑州煦尧软件有限责任公司";
//            String verificationCode = generateVerificationCode();
//            LocalDateTime now = LocalDateTime.now();
//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
//            String timeStr = now.format(formatter);
//
//            String[] params = {verificationCode, "5"};
//            SmsSingleSender sender = new SmsSingleSender(appid, appKey);
//            SmsSingleSenderResult result = sender.sendWithParam("86",login.getUsername(), templateId, params, smsSign, "", "");
//            System.out.println(result);
//
//            String verificationCodes = timeStr + "_" + verificationCode;
//
//            Cache cache = cacheManager.getCache("verificationCodes");
//            cache.put(login.getUsername(), verificationCodes);
//
//
//
//            return Resultd.ok().data("info", "验证码发送成功");
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            return Resultd.error().data("info", "验证码发送失败，请稍后再试");
//        }
//    }
//
//    // 生成一个6位随机数作为验证码
//    private String generateVerificationCode() {
//
//        Random random = new Random();
//        int code = 100000 + random.nextInt(900000);
//        return String.valueOf(code);
//    }
//
//
////    //发送信息
////    @PostMapping("/send")
////    public Resultd sendCode(@RequestBody Login login) {
////        try{
////            if(login.getMode() == 0){//注册
////                if(userMapper.chaxu(login.getUsername()) > 0){
////                    return Resultd.error().data("info", "该手机号已被注册");
////
////                }else {
////                    int appid = 1400912352;
////                    String appKey = "15601612830bbf62d36db75fd532e796";
////                    int templateId = 2162562;
////                    String smsSign = "郑州煦尧软件有限责任公司";
////                    String verificationCode = generateVerificationCode();
////                    LocalDateTime now = LocalDateTime.now();
////                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
////                    String timeStr = now.format(formatter);
////
////                    String[] params = {verificationCode, "5"};
////                    SmsSingleSender sender = new SmsSingleSender(appid, appKey);
////                    SmsSingleSenderResult result = sender.sendWithParam("86", login.getUsername(), templateId, params, smsSign, "", "");
////                    System.out.println(result);
////
////                    String verificationCodes = timeStr + "_" + verificationCode;
////
////                    // 将验证码存储到缓存中
////                    Cache cache = cacheManager.getCache(VERIFICATION_CODES_CACHE_NAME);
////                    if (cache != null) {
////                        cache.put(login.getUsername(), verificationCodes);
////                    }
////
////                    return Resultd.ok().data("info", "验证码发送成功");
////                }
////
////            } else if (login.getMode() == 1) {
////                int appid = 1400912352;
////                String appKey = "15601612830bbf62d36db75fd532e796";
////                int templateId = 2162562;
////                String smsSign = "郑州煦尧软件有限责任公司";
////                String verificationCode = generateVerificationCode();
////                LocalDateTime now = LocalDateTime.now();
////                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
////                String timeStr = now.format(formatter);
////
////                String[] params = {verificationCode, "5"};
////                SmsSingleSender sender = new SmsSingleSender(appid, appKey);
////                SmsSingleSenderResult result = sender.sendWithParam("86", login.getUsername(), templateId, params, smsSign, "", "");
////                System.out.println(result);
////
////                String verificationCodes = timeStr + "_" + verificationCode;
////                return Resultd.ok().data("info", "验证码发送成功");
////            }else{
////                return Resultd.ok().data("info", "无效的验证码模式");
////            }
////        } catch (Exception e) {
////            e.printStackTrace();
////            return Resultd.error().data("info", "验证码发送失败，请稍后再试");
////        }
////
////    }
//
//
//
//}

package com.xiaocikuai.smallchunks.controller.User;

import com.xiaocikuai.smallchunks.entity.User.Login;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import java.util.Random;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import com.xiaocikuai.smallchunks.utils.resultd.Resultd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.web.bind.annotation.*;

// sms 短信服务
@Api(tags = "短信控制器")
@RequestMapping("/api/code")
@CrossOrigin
@RestController
public class SmsController {

    @Autowired
    private JCacheCacheManager cacheManager;

    @Qualifier("userMapper")

    // 发送信息
    @ApiOperation(value = "发送验证码")
    @PostMapping("/send")
    public Resultd sendCode(
            @ApiParam(name = "login", value = "登录信息（包含手机号）", required = true)
            @RequestBody Login login) {
        try {

            int appid = 1400912352;
            String appKey = "15601612830bbf62d36db75fd532e796";
            int templateId = 2162562;
            String smsSign = "郑州煦尧软件有限责任公司";
            String verificationCode = generateVerificationCode();
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            String timeStr = now.format(formatter);

            String[] params = {verificationCode, "5"};
            SmsSingleSender sender = new SmsSingleSender(appid, appKey);
            SmsSingleSenderResult result = sender.sendWithParam("86", login.getUsername(), templateId, params, smsSign, "", "");
            System.out.println(result);

            String verificationCodes = timeStr + "_" + verificationCode;

            Cache cache = cacheManager.getCache("verificationCodes");
            cache.put(login.getUsername(), verificationCodes);

            return Resultd.ok().data("info", "验证码发送成功");

        } catch (Exception e) {
            e.printStackTrace();
            return Resultd.error().data("info", "验证码发送失败，请稍后再试");
        }
    }

    // 生成一个 6 位随机数作为验证码
    @ApiOperation(value = "生成验证码")
    private String generateVerificationCode() {

        Random random = new Random();
        int code = 100000 + random.nextInt(900000);
        return String.valueOf(code);
    }
}